package com;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mail.SendMail;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.connection.jdbconnection;

public class AdminProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	static Connection conn = null;

	PreparedStatement pst = null;
	ResultSet rs = null;
	String aname;
	String designation;
	String contact;
	String doj;
	String image;
	String email;
	
	
	String address;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		jdbconnection db = new jdbconnection();
		Statement st = db.jdbConnection();
		

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String path = getServletContext().getRealPath("photo");

		HttpSession session = request.getSession();
		
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (!isMultipart) {
		} else {
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			List items = null;
			try {
				items = upload.parseRequest(request);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			Iterator itr = items.iterator();
			while (itr.hasNext()) {
				FileItem item = (FileItem) itr.next();
				if (item.isFormField()) {
					String name = item.getFieldName();

					String value = item.getString();
					System.out.println(name + value);
					if (name.equals("aname")) {
						
						aname = value;
						System.out.println(aname);
					}
					if (name.equals("designation")) {
						designation = value;
						System.out.println(designation);
					}
					if (name.equals("contact")) {
						contact = value;
						System.out.println(contact);
					}
					if (name.equals("doj")) {
						doj = value;
						System.out.println(doj);
					}
						if (name.equals("address")) {
							address = value;
						System.out.println(address);
					}
					if (name.equals("email")) {
						email = value;
						System.out.println(email);
					}
					

				} else {
					try {
						String itemName = item.getName();
						String name[] = itemName.split("\\.");
						Random random = new Random();
						int z = random.nextInt(23000);
						String name1 = name[0] + z;
						image = name1 + "." + name[1];
						File savedFile = new File(path + "/" + image);
						image = "photo/" + image;
						item.write(savedFile);
						} catch (Exception e) {
						
						System.out.println("Exception.");
						e.printStackTrace();
					}
					String query = "Insert into adminprofile(name,designation,contact,doj,sign,address,email) values('"
							+ aname + "','" + designation + "','" + contact
							+ "','" + doj + "','" + image + "','" + address
							+ "','" + email +  "')";

					System.out.println(query);
					try {
						int a = st.executeUpdate(query);
						System.out.println(a);
					} catch (SQLException e) {
						
						e.printStackTrace();
					}
					System.out.println("Register Successfully.......");
					session.setAttribute("error", new Object());
					response.sendRedirect("EditProfile.jsp");
					
							

						}
					
				}

			}

		}

	

}
